package com.lb.jwitter.connectDatabase;

import java.sql.*;

public class ConnectMySqlDataBase {
	private  String connectionString;
	private  String userName;
	private  String password;
	private  Connection connection;
	private  Statement st;
	public  Connection getConnection(){
		countOpen++;
		//System.out.println("connect:"+countOpen);
		userName="root";
		password="root";
		String databaseName="thesisproject_dbo";
		connection=null;
		connectionString="jdbc:mysql://localhost:3306/"+databaseName+"?characterEncoding=utf-8";
		try{
			Class.forName("com.mysql.jdbc.Driver");
			connection=DriverManager.getConnection(connectionString, userName, password);
			st=connection.createStatement();
		}
		catch(Exception e){
			System.out.println(e);
		}
		return connection;
	}
	public  ResultSet getData(String query){
		
		ResultSet data=null;
		try {
			data=st.executeQuery(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}
	public  int updateData(String query){
		int result=0;
		try {
			result=st.executeUpdate(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	public void closeConnection(){
		countClose++;
		//System.out.println("close:"+countClose);
		if(connection!=null){
			try {
				st.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public static int countOpen=0;
	public static int countClose=0;
	
}
